package m.system;

import m.common.service.Service;
import m.system.util.ClassUtil;
import m.system.util.SystemUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class RuntimeData {
	private static Map<Class<? extends Service>,Service> serviceData;
	/**
	 * 获取service实例
	 *  <T>
	 *  clazz
	 * 
	 *  MException
	 */
	@SuppressWarnings("unchecked")
	public static <T extends Service> T getService(Class<? extends T> clazz){
		if(null==serviceData){
			serviceData= new HashMap<>();
		}
		T service=(T) serviceData.get(clazz);
		if(null==service){
			service=ClassUtil.newInstance(clazz);
			serviceData.put(clazz, service);
		}
		return service;
	}

	private static String classPath="";
	private static String classWeb="";
	private static String classTmp="";
	private static String publicPath="";
	private static String publicWeb="";
	private static String tableSchema="";
	private static String modelPack="";
	private static String actionPack="";
	private static String taskPack="";
	private static Boolean debug=true;
	private static Boolean outLog=false;
	private static String jksCert="";
	private static String jksPass="";
	/**
	 * 清除临时文件目录
	 */
	public static void clearTmpFile() {
		deleteDir(classTmp);
	}
	private static void deleteDir(String path) {
		File file=new File(path);
		if(file.exists()) {
			File[] fs=file.listFiles();
			if(null!=fs) {
				for(File f : fs) deleteDir(f.getPath());
			}
			file.delete();
		}
	}
	
	public static String getClassPath() {
		return classPath;
	}
	public static String getClassWeb(){return classWeb;}
	public static String getClassTmp(){return classTmp;}
	public static void setClassPath(String classPath) {
		SystemUtil.printlnIf(true,"classPath:",classPath);
		RuntimeData.classPath = classPath;
		RuntimeData.classWeb=classPath+"web/";
		RuntimeData.classTmp=classPath+"tmp/";
	}
	public static String getPublicPath() {
		return publicPath;
	}
	public static String getPublicWeb(){return publicWeb;}
	public static void setPublicPath(String publicPath) {
		SystemUtil.printlnIf(true,"publicPath:",publicPath);
		RuntimeData.publicPath = publicPath;
		RuntimeData.publicWeb=publicPath+"web/";
	}
	public static Boolean getDebug() {
		return debug;
	}
	public static void setDebug(Boolean debug) {
		RuntimeData.debug = debug;
	}
	public static Boolean getOutLog() {
		return outLog;
	}
	public static void setOutLog(Boolean outLog) {
		RuntimeData.outLog = outLog;
	}
	public static String getTableSchema() {
		return tableSchema;
	}
	public static void setTableSchema(String tableSchema) {
		RuntimeData.tableSchema = tableSchema;
	}

	public static String getModelPack() {
		return modelPack;
	}
	public static void setModelPack(String modelPack) {
		RuntimeData.modelPack = modelPack;
	}
	public static String getActionPack() {
		return actionPack;
	}
	public static void setActionPack(String actionPack) {
		RuntimeData.actionPack = actionPack;
	}
	public static String getTaskPack() {
		return taskPack;
	}
	public static void setTaskPack(String taskClass) {
		RuntimeData.taskPack = taskClass;
	}

	public static String getJksCert() {
		return jksCert;
	}

	public static void setJksCert(String jksCert) {
		RuntimeData.jksCert = jksCert;
	}

	public static String getJksPass() {
		return jksPass;
	}

	public static void setJksPass(String jksPass) {
		RuntimeData.jksPass = jksPass;
	}
}
